package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.RecommendGoods;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author: erdonc
 * @date: 2022/9/20 20:38
 * TODO:
 */
@RestController
public class ProductController {

    @Autowired
    IProductService iProductService;


    /**
     * @Author: erdonc
     * @date : 2022/9/20 21:12
     * TODO: 展示全部商品，并根据请求信息进行排序，并分页
     * 商品分类cid  请求 URL: http://124.71.209.81/shopping/goods?page=1&size=20&sort=&priceGt=&priceLte=&cid=202
     */
    @GetMapping("shopping/goods")
    public ResponseData goods(PageRequest pageRequest) {
        // 校验请求参数
        pageRequest.requestCheck();

        // 调用service层业务方法
        AllProductResponse allProduct = iProductService.getAllProduct(pageRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())) {
            System.out.println(ShoppingRetCode.SUCCESS.getCode());
            System.out.println("allProduct.getCode():"+allProduct.getCode());
            List<ProductDto> productDtoList = allProduct.getData();
            Long total = allProduct.getTotal();

            // 封装数据，并响应
            AllProductResponse response = new AllProductResponse();
            response.setData(productDtoList);
            response.setTotal(total);

            return new ResponseUtil<>().setData(response);
        }

        String msg = allProduct.getMsg();
        return new ResponseUtil().setErrorMsg(msg);
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/21 19:54
     * TODO:  显示热门推荐商品
     * 请求 URL: http://124.71.209.81/shopping/recommend
     */
    @GetMapping("shopping/recommend")
    public ResponseData recommend() {
        // 调用service层方法
        Set<PanelDto> recommendGoods = iProductService.getRecommendGoods();

        return new ResponseUtil<>().setData(recommendGoods);
        // 调用service层方法
        // RecommendGoods recommendGoods = iProductService.getRecommendGoods();
        // if (ShoppingRetCode.SUCCESS.getCode().equals(recommendGoods.getCode())) {
        //     PanelDto panelDto = recommendGoods.getPanelDto();
        //     List<PanelDto> panelDtoList = new ArrayList<>();
        //     panelDtoList.add(panelDto);
        //     return new ResponseUtil().setData(panelDtoList);
        // }
        // return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());

    }



}
